<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>C语言基础 - 递归函数</title>
    <link rel="stylesheet" href="common.css">
    <style>
        /* 页面特有样式 */
        .recursion-card {
            background-color: #f5f9ff;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 25px;
            border-left: 4px solid #5D9CEC;
        }
        
        .principle-box {
            background-color: #f0f7ff;
            padding: 15px;
            border-radius: 5px;
            margin: 15px 0;
        }
        
        .example-box {
            background-color: #fff3e0;
            padding: 15px;
            border-radius: 5px;
            margin: 15px 0;
        }
        
        .comparison-box {
            background-color: #e8f5e9;
            padding: 15px;
            border-radius: 5px;
            margin: 15px 0;
        }
        
        .process-diagram {
            background-color: #f8f8f8;
            padding: 15px;
            border-radius: 5px;
            font-family: monospace;
            text-align: center;
            margin: 15px 0;
        }
        
        .warning-note {
            background-color: #ffebee;
            border-left: 4px solid #f44336;
            padding: 12px;
            margin: 15px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>C语言基础入门课程</h1>
        </header>
        
        <nav>
            <a href="index.html">首页</a>
            <a href="02-intro.html">C语言简介与环境搭建</a>
            <a href="03-structure.html" class="current">C程序结构</a>
            <a href="04-datatypes.html">基本数据类型</a>
            <a href="05-variables.html">变量与常量</a>
            <a href="06-operators.html">运算符</a>
            <a href="07-io.html" class="current">输入输出</a>
            <a href="08-conditionals.html">条件语句</a>
            <a href="09-loops.html">循环结构</a>
            <a href="10-functions.html" class="current">函数基础</a>
            <a href="11-scope.html">作用域与存储类</a>
            <a href="12-recursion.html">递归函数</a>
            <a href="13-arrays.html">一维数组</a>
            <a href="14-multidim.html" class="current">多维数组</a>
            <a href="15-strings.html">字符串处理</a>
            <a href="16-pointers.html">指针基础</a>
            <a href="17-pointers_advanced.html" class="current">指针进阶</a>
            <a href="18-structs.html">结构体</a>
            <a href="19-files.html">文件操作</a>
            <a href="20-project.html">综合项目</a>
        </nav>
        
        <main>
            <h2>C语言递归函数</h2>
            
            <section>
                <h3>递归概述</h3>
                <p>递归是一种函数调用自身的编程技巧，适用于问题可以分解为相同类型的子问题的情况。</p>
            </section>
            
            <div class="recursion-card">
                <h3>1. 递归原理</h3>
                
                <div class="principle-box">
                    <h4>递归的两个关键要素</h4>
                    <ol>
                        <li><strong>基本情况(Base Case)</strong>：递归终止的条件</li>
                        <li><strong>递归情况(Recursive Case)</strong>：函数调用自身的条件</li>
                    </ol>
                    
                    <h4>递归调用过程</h4>
                    <div class="process-diagram">
                        <pre>
调用层级1
  ↓
调用层级2
  ↓
调用层级3
  ↓
...
  ↓
基本情况 → 开始返回结果
                        </pre>
                    </div>
                </div>
                
                <div class="warning-note">
                    <h4>注意事项</h4>
                    <ul>
                        <li>必须有明确的终止条件</li>
                        <li>每次递归调用应使问题规模减小</li>
                        <li>递归层次过深可能导致栈溢出</li>
                    </ul>
                </div>
            </div>
            
            <div class="recursion-card">
                <h3>2. 阶乘示例</h3>
                
                <div class="example-box">
                    <h4>数学定义</h4>
                    <p>n! = n × (n-1) × ... × 1</p>
                    <p>0! = 1 (基本情况)</p>
                    
                    <h4>递归实现</h4>
                    <pre><code>#include &lt;stdio.h&gt;

// 递归计算阶乘
long factorial(int n) {
    // 基本情况
    if (n == 0 || n == 1) {
        return 1;
    }
    // 递归情况
    return n * factorial(n - 1);
}

int main() {
    int num = 5;
    printf("%d! = %ld\n", num, factorial(num));
    return 0;
}</code></pre>
                </div>
                
                <div class="process-diagram">
                    <h4>factorial(5)的执行过程</h4>
                    <pre>
factorial(5)
5 * factorial(4)
5 * (4 * factorial(3))
5 * (4 * (3 * factorial(2)))
5 * (4 * (3 * (2 * factorial(1))))
5 * (4 * (3 * (2 * 1))) = 120
                    </pre>
                </div>
            </div>
            
            <div class="recursion-card">
                <h3>3. 递归优缺点</h3>
                
                <div class="comparison-box">
                    <h4>优点</h4>
                    <ul>
                        <li>代码简洁，表达力强</li>
                        <li>适合解决分治问题(如树、图遍历)</li>
                        <li>自然反映问题的递归定义</li>
                    </ul>
                    
                    <h4>缺点</h4>
                    <ul>
                        <li>效率较低，函数调用有开销</li>
                        <li>可能消耗大量栈空间</li>
                        <li>调试较困难</li>
                    </ul>
                </div>
                
                <div class="example-box">
                    <h4>斐波那契数列示例</h4>
                    <pre><code>// 递归实现(效率低)
int fibonacci(int n) {
    if (n <= 1) return n;
    return fibonacci(n-1) + fibonacci(n-2);
}

// 迭代实现(推荐)
int fibonacci_iter(int n) {
    if (n <= 1) return n;
    
    int a = 0, b = 1, c;
    for (int i = 2; i <= n; i++) {
        c = a + b;
        a = b;
        b = c;
    }
    return b;
}</code></pre>
                </div>
                
                <div class="best-practice">
                    <h4>使用建议</h4>
                    <ul>
                        <li>适合递归的问题：汉诺塔、目录遍历、快速排序等</li>
                        <li>对于线性递归(如阶乘)，可考虑尾递归优化</li>
                        <li>性能关键代码建议使用迭代</li>
                        <li>递归深度较大时考虑显式栈实现</li>
                    </ul>
                </div>
            </div>
            
            <section>
                <h3>综合示例：汉诺塔问题</h3>
                <pre><code>#include &lt;stdio.h&gt;

void hanoi(int n, char from, char to, char aux) {
    if (n == 1) {
        printf("将盘子从 %c 移动到 %c\n", from, to);
        return;
    }
    hanoi(n-1, from, aux, to);
    printf("将盘子从 %c 移动到 %c\n", from, to);
    hanoi(n-1, aux, to, from);
}

int main() {
    int disks = 3;
    printf("汉诺塔 %d 个盘子的解法:\n", disks);
    hanoi(disks, 'A', 'C', 'B');
    return 0;
}</code></pre>
            </section>
        </main>
        
        <footer>
             <p>&copy; </p>
        </footer>
        
        <!-- 二维码区域 -->

    </div> <!-- 结束.container -->
</body>
</html>